package com.m2bshop.shop.controller;

import com.m2bshop.shop.dao.PayorderDao;
import com.m2bshop.shop.dao.WalletMapper;
import com.m2bshop.shop.pojo.JmfOrder;
import com.m2bshop.shop.pojo.Wallet;
import com.m2bshop.shop.service.JmfOrderService;
import com.m2bshop.shop.service.WalletService.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/JmfOrder")
public class JmfOrderController {
    @Autowired
    private JmfOrderService jmfOrderService;
    @Autowired
    private WalletMapper walletMapper;
    @Autowired
    private PayorderDao payorderDao;
    @Autowired
    private WalletService walletService;

    @RequestMapping("/jmfdzf")
    public String findAwaitingPayOrder(Model model){
        List<JmfOrder> jmfOrderList = jmfOrderService.findAwaitingPayOrder();
        model.addAttribute("jmfOrderList",jmfOrderList);
        return "jmfunpayorder";
    }

    @RequestMapping("/jmfdfh")
    public String findAwaitingShipmentOrder(Model model){
        List<JmfOrder> jmfOrderList = jmfOrderService.findAwaitingShipOrder();
        model.addAttribute("jmfOrderList",jmfOrderList);
        return "jmfunshiporder";
    }
    @RequestMapping("/jmfyfh")
    public String findShipedmentOrder(Model model){
        List<JmfOrder> jmfOrderList = jmfOrderService.findshipedOrder();
        model.addAttribute("jmfOrderList",jmfOrderList);
        return "jmfshipedorder";
    }
    @RequestMapping("/jmfywc")
    public String findCompletedOrder(Model model){
        List<JmfOrder> jmfOrderList = jmfOrderService.findCompletedOrder();
        model.addAttribute("jmfOrderList",jmfOrderList);
        return "jmfcompletedorder";
    }
    @RequestMapping("/jmfyqx")
    public String findCancelledOrder(Model model){
        List<JmfOrder> jmfOrderList = jmfOrderService.findCancelledOrder();
        model.addAttribute("jmfOrderList",jmfOrderList);
        return "jmfcancelledorder";
    }

    @RequestMapping("/Jmfunpayfindbytitle")
    public String findJmfUnpayOrderByTitle(Model model, HttpServletRequest httpServletRequest){
        String title = httpServletRequest.getParameter("search");
        System.out.println(title);
        List<JmfOrder> jmfOrderList = jmfOrderService.findJmfOrderByTitle(title);
        model.addAttribute("jmfOrderList",jmfOrderList);
        return "jmfunpayorder";
    }

    @RequestMapping("/Jmfunshipfindbytitle")
    public String findJmfUnshipOrderByTitle(Model model, HttpServletRequest httpServletRequest){
        String title = httpServletRequest.getParameter("search");
        System.out.println(title);
        List<JmfOrder> jmfOrderList = jmfOrderService.findJmfOrderByTitle(title);
        model.addAttribute("jmfOrderList",jmfOrderList);
        return "jmfunshiporder";
    }

    @RequestMapping("/Jmfshipedfindbytitle")
    public String findJmfShipedOrderByTitle(Model model, HttpServletRequest httpServletRequest){
        String title = httpServletRequest.getParameter("search");
        System.out.println(title);
        List<JmfOrder> jmfOrderList = jmfOrderService.findJmfOrderByTitle(title);
        model.addAttribute("jmfOrderList",jmfOrderList);
        return "jmfshipedorder";
    }

    @RequestMapping("/Jmfcompletedfindbytitle")
    public String findJmfCompletedOrderByTitle(Model model, HttpServletRequest httpServletRequest){
        String title = httpServletRequest.getParameter("search");
        System.out.println(title);
        List<JmfOrder> jmfOrderList = jmfOrderService.findJmfOrderByTitle(title);
        model.addAttribute("jmfOrderList",jmfOrderList);
        return "jmfcompletedorder";
    }
    @RequestMapping("/Jmfcancelledfindbytitle")
    public String findJmfCancelledOrderByTitle(Model model, HttpServletRequest httpServletRequest){
        String title = httpServletRequest.getParameter("search");
        System.out.println(title);
        List<JmfOrder> jmfOrderList = jmfOrderService.findJmfOrderByTitle(title);
        model.addAttribute("jmfOrderList",jmfOrderList);
        return "jmfcancelledorder";
    }
    @RequestMapping("/trackpay/{id}")
    public String paymoney(@PathVariable("id") int id, HttpSession httpSession,String payment){
        System.out.println(payment);
        System.out.println(id);
        Integer userid = Integer.parseInt(httpSession.getAttribute("userId")+"");
        Wallet wallet = walletMapper.getInfo(userid);
        String truePayment = wallet.getPayment();
        if (truePayment.equals(payment)){
            JmfOrder jmfOrder = payorderDao.selectById(id);
            double total = jmfOrder.getJtotal();
            double balance = wallet.getBalance();
            System.out.println(total);
            System.out.println(balance);
            if (balance >= total ){
                //扣钱
                String result= walletService.UpdateBalanceByExpense(userid,total);
                if(result.equals("支付成功")) {
                    jmfOrder.setStatus("待发货");
                    Random r =new Random();
                    int random = (int)(Math.random()*100);
                    jmfOrder.setTrackid(164849846+random);
                    payorderDao.updateById(jmfOrder);

                    //给卖家加钱
                    Wallet wallet1 = walletMapper.getInfo(jmfOrder.getSid());
                    double balance1 = wallet1.getBalance();
                    double newbalance = jmfOrder.getJtotal() + balance1;
                    wallet1.setBalance(newbalance);
                    walletMapper.Update(wallet1);
                    System.out.println("卖家到账"+jmfOrder.getJtitle());
                }

            }else {
                System.out.println("未支付成功！");
            }
        }else {
            System.out.println("密码错误！");
            //密码错误！
        }
        return "redirect:/JmfOrder/jmfdzf";
    }
}
